package com.pengcheng.taskapplication.mvp.view.fragment;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import com.pengcheng.taskapplication.R;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link ShowSettingDialogFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link ShowSettingDialogFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ShowSettingDialogFragment extends DialogFragment implements View.OnClickListener{
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "model";
    // TODO: Rename and change types of parameters
    private TextView tv_dismiss,tv_setting;

    private OnFragmentInteractionListener mListener;

    public ShowSettingDialogFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment IntegralDialogFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static ShowSettingDialogFragment newInstance() {
        ShowSettingDialogFragment fragment = new ShowSettingDialogFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {

        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getDialog().setCanceledOnTouchOutside(false);
        getDialog().getWindow().getAttributes().windowAnimations=R.style.BottomAnimation;
        View rootView=inflater.inflate(R.layout.fragment_dialog_show, container, false);
        tv_dismiss=(TextView)rootView.findViewById(R.id.tv_dimiss);
        tv_setting=(TextView)rootView.findViewById(R.id.tv_setting);
        tv_setting.setOnClickListener(this);
        tv_dismiss.setOnClickListener(this);
        return rootView;
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onConfirm() {
        if (mListener != null) {
            mListener.onConfirm();
        }
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onCancel() {
        if (mListener != null) {
            mListener.onCancel();
        }
    }
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_dimiss:
                onCancel();
                dismiss();
                break;
            case R.id.tv_setting:
                onConfirm();
                dismiss();
                break;
        }

    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onConfirm();
        void onCancel();
    }
}
